﻿using System;
using System.Collections.Generic;
using System.Web;

/// <summary>
/// Summary description for CarRegNo
/// </summary>
/// 
namespace CE.CarInsurance
{
    public class CarRegNo
    {
        public CarRegNo()
        {
            //
            // TODO: Add constructor logic here
            //
        }

        public bool CheckVehicleNumber(string vehicleNo)
        {
            //* S - Private vehicle (since 1984)
            //* BA - Alphabetical series
            //* 1234 - Numerical series
            //* A - Checksum letter (the characters "F", "I", "N", "O", "Q", "V" and "W" are never used as check digits.)

            bool _retVal = false;
            try
            {
                //A = 1, B = 1, C = 3, ..,etc
                char[] arrNormalChars = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K',
                                    'L','M','N','O','P','Q','R','S','T','U','V',
                                    'W','X','Y','Z'};

                //Last Characters and Positions
                //Eg. A = 1, Y = 2, U = 3, ..,etc
                char[] arrCheckChars = { 'A', 'Y', 'U', 'S', 'P', 'L', 'J', 'G', 'D', 'B', 'Z', 
                                   'X', 'T', 'R', 'M', 'K', 'H', 'E', 'C' };


                //Connstant Numbers to Multiply by Position
                int[] arrContNums = { 3, 14, 2, 12, 2, 11, 1 };
                //Eg, SBA1234G, 
                //S=19 then 19 * 3 = 54
                //B=2 then 2 * 14 = 28
                //A=1 then 1 * 2 = 2
                //1 then 1 * 12 = 12
                //2 then 2 * 2 = 4
                //3 then 3 * 11 = 33
                //4 then 4 * 1 = 4
                //END Connstant Numbers

                vehicleNo = vehicleNo.Replace(" ", "");

                string _tempVNo = vehicleNo.Trim().ToUpper();

                char[] _arrVNChars = _tempVNo.ToCharArray();

                char _lastChar = Convert.ToChar(_arrVNChars.GetValue(_arrVNChars.Length - 1));

                int _result = 0;
                int _tempValue = 0;
                int[] _arrTempValue = new int[_arrVNChars.Length - 1];
                for (int i = 0; i < _arrVNChars.Length - 1; i++)
                {
                    int _tempAsciiValue = Convert.ToInt32(_arrVNChars[i]);
                    if (_tempAsciiValue >= 65 && _tempAsciiValue <= 90)
                    {
                        for (int n = 0; n < arrNormalChars.Length; n++)
                        {
                            if (_arrVNChars[i] == arrNormalChars[n])
                            {
                                _tempValue += ((n + 1) * arrContNums[i]);
                                _arrTempValue[i] = ((n + 1) * arrContNums[i]);
                                break;
                            }
                        }
                    }
                    else if (_tempAsciiValue >= 48 && _tempAsciiValue <= 57)
                    {
                        _tempValue += (Convert.ToInt32(_arrVNChars[i].ToString()) * arrContNums[i]);
                        _arrTempValue[i] = (Convert.ToInt32(_arrVNChars[i].ToString()) * arrContNums[i]);
                    }
                }

                //_result = Convert.ToInt32(_tempValue / 19);
                //_result = _tempValue - (_result * 19);
                _result = _tempValue % 19;

                if (arrCheckChars[_result] == _lastChar)
                {
                    _retVal = true;
                }
            }
            catch
            {
            }

            return _retVal;
        }
    }
}
